package doan.chuyennganh.util;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import doan.chuyennganh.model.ListStudentReturnObject;
import doan.chuyennganh.model.Student;

public class JsonHelper {
	public static ListStudentReturnObject parseListStudent(String json) throws JSONException {
		ListStudentReturnObject list = new ListStudentReturnObject();

		JSONObject obj = new JSONObject(json);

		list.setMessage(obj.getString("message"));
		list.setError_code(Integer.parseInt(obj.getString("error_code")));
		JSONArray data = obj.getJSONArray("data");
		int length = data.length();
		ArrayList<Student> students = new ArrayList<Student>();
		for (int j = 0; j < length; j++) {
			JSONObject st = data.getJSONObject(j);
			Student tmp = new Student(st.getString("masv"),
					st.getString("hoten"), st.getString("lop"));
			students.add(tmp);
		}
		list.setData(students);

		return list;
	}
}
